import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { user_info } from '../login/login';



/**
 * Generated class for the NicknameEditPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-nickname-edit',
  templateUrl: 'nickname-edit.html',
})
export class NicknameEditPage {

	
	oldname: any;
	member_id:any;
	phone:any;
	//用户信息
	userInfo: user_info = new user_info();

  constructor(
		public navCtrl: NavController,
		public viewCtrl: ViewController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public user: UserInfoProvider) {}


  ionViewDidLoad() {
		console.log('ionViewDidLoad NicknameEditPage');
		this.userInfo=this.user.getuserInfo();
		this.member_id=this.userInfo.member_id;
		this.oldname=this.userInfo.nickname;
		this.phone=this.userInfo.phone;
		// this.storage.getStorage("login_info").then(res => {
		// 	if(res != undefined) {
		// 		this.member_id=res['member_id']	;
		// 		this.oldname =res['nickname'];
		// 		this.phone =res['phone'];
		// 	}
		// });
		
  }



	saveNickname() {
		if(this.oldname == "" || this.oldname == null) {
			this.dialog.showAlert("请输入昵称", "");
		} else {
		var params = {
			// batch: [
			// 	{
					//member_id: this.member_id,
					nickname: this.oldname
			// 	}
			// ]
			
		}
		this.http.postJson('', 'member/member/update?id='+this.member_id, params
		, '').then(res => {
			if(res['status'] == 200) {
				this.dialog.showAlert(res['message'], '');
				this.login().then( res => (this.navCtrl.setRoot('CenterPage')) );
				
			}
			
		})
	}

	}

	 /**
   * 获取用户信息
   */
  async login() {
    //   await this.storage.getStorage("login_info").then(res => {
    // 	if(res != undefined) {
    // 		this.userInfo = res;
    // 	}
    // });;
    console.log(this.userInfo);
    const res = await this.http.postJson('', 'member/member/view?id=' + this.userInfo.member_id, {}, '')
      .then(res => {
        console.log(res)
        if (res['status'] == 200) {
		  res['data']['password'] = this.userInfo.password;
		  res['data']['isAutomaticLogon'] = this.userInfo.isAutomaticLogon;
          this.userInfo = res['data'];
		  this.user.setuserInfo(this.userInfo);
		 
          // this.storage.saveStorage("login_info", res['data']);
          // this.http.setAuthorization(res['data']['access_token']);//该令牌唯一表示该用户、用户的组和用户的特权。系统使用令牌控制用户可以访问哪些安全对象，并控制用户执行相关系统操作的能力
          // this.navCtrl.push('CenterPage');
        }
      });
  }
	
}



